package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import po.Course;
import po.Result;
import service.CourseService;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @RequestMapping("/getCourseInfoView")
    public String getCourseInfoView() {
        return "course/courseInfo";
    }

    @RequestMapping("/findCourses")
    public @ResponseBody
    List<Course> findCourses(Course course, HttpSession session) throws Exception {
        course.setTeacherNumber((String) session.getAttribute("userNumber"));
        return courseService.findCourses(course);
    }

    @RequestMapping("/getAddCourseView")
    public String getAddCourseView() {
        return "course/addCourse";
    }

    @RequestMapping("/addCourse")
    public @ResponseBody
    Result addCourse(Course course) {
        return courseService.addCourse(course);
    }

    @RequestMapping("/deleteCourse")
    public @ResponseBody
    Result deleteCourse(int number) {
        return courseService.deleteCourseByNumber(number);
    }

    @RequestMapping("/getEditCourseView")
    public ModelAndView getEditCourseView(Course course, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        course.setTeacherNumber((String) session.getAttribute("userNumber"));
        course.setTeacherName((String) session.getAttribute("userName"));
        modelAndView.addObject("course", course);
        modelAndView.setViewName("course/editCourse");
        return modelAndView;
    }

    @RequestMapping("/updateCourse")
    public @ResponseBody
    Result updateCourse(Course course) {
        return courseService.updateCourseByNumber(course);
    }
}
